package 代码随想录_回溯.力扣_回溯.岛屿系列;

/**
 * @author zx
 * @create 2022-08-12 22:42
 */
public class 岛屿的最大面积_695 {
    public int maxAreaOfIsland(int[][] grid) {
        int res = 0;
        for (int i = 0; i < grid.length; i++) {
            for (int j = 0; j < grid[i].length; j++) {
                if (grid[i][j] == 1) {
                    int s = dfs(grid, i, j);
                    res = Math.max(res, s);//取最大值
                }
            }
        }
        return res;
    }
    //求每个岛屿的面积
    private int dfs(int[][]grid, int i, int j) {
        if(i < 0 || j < 0 || i >= grid.length || j >= grid[0].length){
            return 0;
        }
        if(grid[i][j] != 1){
            return 0;
        }
        grid[i][j] = 2;
        return 1 + dfs(grid,i - 1, j) + dfs(grid, i + 1, j)
                + dfs(grid, i, j - 1) + dfs(grid, i, j + 1);

    }
}
